<template>
  <div>
    <div class="company" v-if="company?.title">
      <div class="header">
        <div class="first">
          <div class="left" @click.stop="$router.go(-1)">
            <img src="@/../public/images/1671779704879.png" alt="" />
          </div>
          <div
            class="right"
            @click.stop="sloveCompanyHandler(text == '关注' ? 0 : 1)"
          >
            <span>+</span><span>{{ text }}</span>
          </div>
        </div>
        <div class="company-name">{{ company.title }}</div>
        <div class="company-img">
          <img :src="company.logoUrl" alt="" />
        </div>
      </div>
      <div class="content">
        <div class="first">
          <div class="first-title">
            {{ company?.companyRange || "暂无" }} |
            {{ company?.companyNumber || 0 }} | {{ company?.companyType }}
          </div>
          <div class="info">
            {{ company?.description || "暂无描述" }}
          </div>
        </div>
        <div class="second">
          <div class="title">工作时间</div>
          <div class="info">
            <span v-for="(work, index) in company?.workTime" :key="index"
              >{{ work }}&nbsp;&nbsp;</span
            >
          </div>
        </div>
        <div class="three">
          <div class="title">员工福利</div>
          <div
            class="item"
            v-for="(welfare, index) in company?.welfare"
            :key="index"
          >
            <div class="img-box">
              <img :src="welfare?.img" alt="" />
            </div>
            <div class="right">
              <div>{{ welfare?.title }}</div>
              <div class="info">
                {{ welfare?.descp }}
              </div>
            </div>
          </div>
        </div>
        <div class="four">
          <div class="title">公司官网</div>
          <div class="text">{{ company.siteUrl }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import qs from "qs";
export default {
  name: "CompanyDetailView",
  data: () => {
    return {
      company: null,
      text: "关注",
      msg:""
    };
  },
  computed: {
    ...mapState(["user", "companyCollection"]),
  },
  watch: {
    companyCollection(list) {
      try {
        [].forEach.call(list, (item) => {
          if (item._id == this.company._id) {
            this.text = "已关注";
            throw new Error("err");
          }
        });
      } catch (err) {
        err;
      }
    },
  },
  methods: {
    //处理收藏公司
    sloveCompanyHandler(type) {
      console.log(type);
      if (!this.user?._id) {
        this.$router.push("/login");
        return;
      }
      this.axios({
        method: "post",
        url:
          type == 0
            ? "/v1/company/company-fav-add"
            : "/v1/company/company-fav-remove",
        data: qs.stringify({
          companyid: this.company._id,
        }),
        headers: {
          Authorization: "Bearer " + localStorage.getItem("token"),
        },
      }).then((res) => {
        if (res.data.statusCode == 200) {
          this.msg = res.data.message;
          this.$toast(`${this.text == "关注" ? "关注" : "取消关注"}成功`);
          this.text = type == 0 ? "已关注" : "关注";
          this.axios({
            method: "post",
            url: "/v1/company/company-fav-list",
            headers: {
              Authorization: "Bearer " + localStorage.getItem("token"),
            },
          }).then((res) => {
            this.$toast(this.msg);
            if (res.data.statusCode == 200) {
              this.$store.commit("setJobCollection", {
                companyCollection: res.data.data.result,
              });
            } else this.$toast("获取信息失败");
          });
        } else this.$toast("业务繁忙中");
      });
    },
  },
  created() {
    this.axios
      .get(
        `https://yw.52kfw.cn/api/v1/company/company-info?companyid=${this.$route?.query?.id}`
      )
      .then((res) => {
        if (res.data.statusCode == 200) {
          this.company = res.data.data;
          (() => {
            try {
              [].forEach.call(this.companyCollection, (item) => {
                if (item._id == this.company._id) {
                  console.log("this");
                  this.text = "已关注";
                  throw new Error("err");
                }
              });
            } catch (err) {
              err;
            }
          })();
        }
      });
  },
};
</script>

<style lang="scss" scoped>
.company {
  .header {
    position: sticky;
    z-index: 100;
    left: 0;
    top: 0;
    width: 100%;
    height: 2rem;
    background-image: url(@/../public/images/1671787342920.png);
    background-size: cover;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    align-content: space-between;
    box-sizing: border-box;
    padding: 0.3rem;
    padding-bottom: 0.2rem;
    .first {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        width: 0.15rem;
        height: 0.28rem;
      }
      .right {
        width: 1.2rem;
        height: 0.5rem;
        border: 0.01rem solid #ffffff;
        border-radius: 0.25rem;
        font-size: 0.24rem;
        color: #ffffff;
        display: flex;
        justify-content: center;
        align-items: center;
        > span {
          font-size: 0.22rem;
          margin-right: 0.1rem;
        }
      }
    }
    .company-name {
      padding-left: 1.7rem;
      font-size: 0.4rem;
      font-weight: bold;
      color: #ffffff;
      box-sizing: border-box;
    }
    .company-img {
      position: absolute;
      width: 1.4rem;
      height: 1.4rem;
      border-radius: 0.2rem;
      overflow: hidden;
      left: 0.3rem;
      bottom: -0.55rem;
    }
  }
  .content {
    text-align: start;
    padding: 0 0.2rem 0 0.3rem;
    .title {
      font-size: 0.3rem;
      font-weight: bold;
      color: #333333;
    }
    .first {
      width: 5.22rem;
      margin-left: 1.7rem;
      margin-top: 0.3rem;
      .first-title {
        font-size: 0.2rem;
        color: #333333;
      }
      .info {
        width: 120%;
        margin-top: 0.18rem;
        font-size: 0.18rem;
        line-height: 0.4rem;
        color: #333333;
        opacity: 0.7;
        transform-origin: 0 0 0;
        transform: scale(0.8);
      }
    }
    .info {
      font-size: 0.24rem;
      color: #333333;
      margin: 0.4rem 0;
      opacity: 0.7;
    }
    .second {
      margin-top: -0.4rem;
      .info {
        font-size: 0.24rem;
        color: #333333;
        margin: 0.4rem 0;
        opacity: 0.7;
      }
    }
    .three {
      .item {
        display: flex;
        margin: 0.4rem 0;
        .img-box {
          width: 0.5rem;
          min-width: 0.5rem;
          height: 0.58rem;
          margin-left: 0.3rem;
          margin-right: 0.6rem;
        }
        .right {
          font-size: 0.28rem;
          font-weight: bold;
          color: #333333;
          .info {
            margin: 0.2rem 0;
            font-weight: normal;
            line-height: 0.4rem;
          }
        }
      }
    }
    .four {
      .text {
        width: 100%;
        word-wrap: break-word;
        font-size: 0.3rem;
        color: #aaa;
        margin: 0.4rem 0;
      }
    }
  }
}
</style>